﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Text;
namespace SIP
{
    public partial class ctrlFecha : System.Web.UI.UserControl
    {
        public CultureInfo Culture { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Culture == null)
                this.Culture = CultureInfo.CurrentCulture;

            this.dateMaskedEditExtender.CultureName = this.Culture.Name;
            this.cexCalendarExtender.Format = this.Culture.DateTimeFormat.ShortDatePattern;
            var csType = this.GetType();
            var csName = this.ClientID + "_ClientValidator";

            var sb = new StringBuilder();

            sb.AppendLine("<script type=\"text/javascript\">");
            sb.AppendLine("function " + csName + "(sender, args) {");
            sb.AppendLine("var year = args.Value.split('/')[2];");
            sb.AppendLine("if (year.substr(0, 2) == '00')");
            sb.AppendLine(" $get('" + this.txtDateTime.ClientID + "').value = '" + this.dateMaskedEditValidator.MinimumValue + "';");
            sb.AppendLine("}");
            sb.AppendLine("</script>");

            ScriptManager.RegisterClientScriptBlock(this, csType, csName, sb.ToString(), false);

            this.dateMaskedEditValidator.ClientValidationFunction = csName;
        }

        public string ValidationGroup
        {
            get { return this.txtDateTime.ValidationGroup; }
            set
            {
                this.txtDateTime.ValidationGroup = value;
                this.dateMaskedEditValidator.ValidationGroup = value;
            }
        }
        public bool IsRequired
        {
            set { this.dateMaskedEditValidator.IsValidEmpty = !value; }
        }

        public bool Enabled
        {
            get { return this.txtDateTime.Enabled; }
            set
            {
                this.txtDateTime.Enabled = value;
                this.cexCalendarExtender.Enabled = value;
                this.cexCalendarExtender.EnabledOnClient = value;
                this.ImgCalendario.Enabled = value;
            }
        }
        public DateTime Value
        {
            get
            {
                if (String.IsNullOrEmpty(this.txtDateTime.Text))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    DateTime date;
                    DateTime.TryParse(this.txtDateTime.Text, out date);
                    return date;
                }
            }
            set
            {
                this.txtDateTime.Text = value.ToShortDateString();
            }
        }


    }
}
